<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Class Marketing_Combination 组合产品
 */
class Controller_Pc_Api_V2_Combination extends Controller_Pc_Api_V2_Base
{
    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Combination
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Combination
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('combination', $this->request_params);

        // 服务
        $this->service = new Service_Api_Combination();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 获取搜索页筛选条件
    public function action_query_condition()
    {
        try {
            $this->result['data'] = $this->service->query_condition();
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 产品搜索
    public function action_query()
    {
        try {
            $this->validation->query()->check();

            $this->request_params['platform_no']=$this->platform_no;
            $this->result['data'] = $this->service->search($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取产品详细信息
    public function action_get_detail()
    {
        try {
            $this->validation->get_detail()->check();

            $this->request_params['platform_no']=$this->platform_no;
            $this->result['data'] = $this->service->info($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取关联产品套餐信息
    public function action_get_suit()
    {
        try {
            $this->validation->get_suit()->check();

            $this->result['data'] = $this->service->get_suit($this->request_params['id']);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取对应套餐的日历报价
    public function action_calendar_price()
    {
        try {
            $this->validation->calendar_price()->check();

            $this->result['data'] = $this->service->calendar_price($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 添加订单
    public function action_add_order()
    {
        try {
            $this->validation->add_order()->check();

            $line    = Arr::get($this->request_params, 'line', '');
            $hotel   = Arr::get($this->request_params, 'hotel', '');
            $car     = Arr::get($this->request_params, 'car', '');
            $spot    = Arr::get($this->request_params, 'spot', '');
            $outdoor = Arr::get($this->request_params, 'outdoor', '');
            $tourist = Arr::get($this->request_params, 'tourist', '');

            if (!$line && !$hotel && !$car && !$spot && !$outdoor) {
                throw new Exception('请选择套餐');
            }

            $use_tourer = Model_Sysconfig::get_configs(0, 'cfg_plugin_combination_usetourer', true);
            if ($use_tourer == 1 && !$tourist) {
                throw new Exception('请添加游客');
            }

            $this->request_params['member_info'] = $this->member_info;
            $this->result['data']                = $this->service->create_order($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 订单详情接口
    public function action_order_detail()
    {
        try {
            $this->validation->order_detail()->check();

            $this->result['data'] = $this->service->order_detail($this->mid, $this->request_params['id']);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取组合公告
    public function action_get_notice()
    {
        try {
            $this->result['data'] = $this->service->get_notice();
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }
}